import java.awt.*;
import java.awt.event.*;

import javax.swing.*;
import javax.swing.event.*;

public class ButtonPanel extends Panel implements ActionListener, ChangeListener
{
	private static final long serialVersionUID = 1L;
	private ImageIcon imgDealerButton, imgSmallBlind, imgBigBlind;
	private JButton allIn, bet, fold, check, call, raise;
	private JLabel tokens, specialF, betValue;
	private JSlider betSlider;
	private JPanel pBet;
	private int myTokens, myBet, maxBet, smallBlind;
	BadugiClient bc;
	ButtonPanel(BadugiClient bc)
	{
		setBackground(new Color(0,172,15));
		this.bc=bc;
		setLayout(new FlowLayout(FlowLayout.LEADING, 1, 0));
		imgDealerButton = new ImageIcon(getClass().getResource("img/dealer_button50.png"));
		imgSmallBlind = new ImageIcon(getClass().getResource("img/small_blind50.png"));
		imgBigBlind = new ImageIcon(getClass().getResource("img/big_blid50.png"));
		pBet = new JPanel();
		pBet.setLayout(new GridLayout(2,1));
		betSlider = new JSlider();
		betSlider.addChangeListener(this);
		betValue = new JLabel();
		betValue.setPreferredSize(new Dimension(100, 25));
		betValue.setFont(new Font("Lucida Console", 0, 22));
		allIn = new JButton("All In");
		allIn.addActionListener(this);
		allIn.setPreferredSize(new Dimension(120, 50));
		allIn.setFont(new Font("Lucida Console", 0, 22));
		fold = new JButton("Fold");
		fold.addActionListener(this);
		fold.setPreferredSize(new Dimension(100, 50));
		fold.setFont(new Font("Lucida Console", 0, 22));
		bet = new JButton("Bet");
		bet.addActionListener(this);
		bet.setPreferredSize(new Dimension(100, 50));
		bet.setVisible(false);
		bet.setFont(new Font("Lucida Console", 0, 22));
		check = new JButton("Check");
		check.addActionListener(this);
		check.setPreferredSize(new Dimension(100, 50));
		check.setVisible(false);
		check.setFont(new Font("Lucida Console", 0, 22));
		call = new JButton("Call");
		call.addActionListener(this);
		call.setPreferredSize(new Dimension(100, 50));
		call.setFont(new Font("Lucida Console", 0, 22));
		call.setVisible(false);
		raise = new JButton("Raise");
		raise.addActionListener(this);
		raise.setPreferredSize(new Dimension(100, 50));
		raise.setFont(new Font("Lucida Console", 0, 22));
		raise.setVisible(false);
		tokens = new JLabel("");
		tokens.setPreferredSize(new Dimension(100, 50));
		tokens.setFont(new Font("Lucida Console", 0, 22));
		specialF = new JLabel("");
		specialF.setPreferredSize(new Dimension(100, 50));
		specialF.setVisible(true);
		pBet.setBackground(new Color(0,172,15));
		betSlider.setBackground(new Color(0,172,15));
		betSlider.setEnabled(false);
		allIn.setEnabled(false);
		fold.setEnabled(false);
		add(pBet);
		pBet.add(betSlider);
		pBet.add(betValue);
		add(allIn);
		add(fold);
		add(bet);
		add(check);
		add(call);
		add(raise);
		add(tokens);
		add(specialF);
	}
	public void actionPerformed(ActionEvent e)
	{
		if(e.getSource()==allIn)
		{
			betSlider.setValue(myTokens);
			betValue.setText(myTokens+"");
			tokens.setText("0");
			myTokens=0;
			bc.allIn();
			
		}
		else if(e.getSource()==fold)
		{
			bc.fold();
		}
		else if(e.getSource()==bet)
		{
			myTokens-=(betSlider.getValue()-myBet);
			myBet=betSlider.getValue();
			bc.bet(betSlider.getValue());
		}
		else if(e.getSource()==check)
		{
			bc.check();
		}
		else if(e.getSource()==call)
		{
			myTokens-=(betSlider.getValue()-myBet);
			myBet=betSlider.getValue();
			bc.call(betSlider.getValue());
		}
		else if(e.getSource()==raise)
		{
			myTokens-=(betSlider.getValue()-myBet);
			myBet=betSlider.getValue();
			bc.raise(betSlider.getValue());
		}
		allIn.setEnabled(false);
		fold.setEnabled(false);
		bet.setEnabled(false);
		check.setEnabled(false);
		call.setEnabled(false);
		raise.setEnabled(false);
		betSlider.setEnabled(false);
	}
	public void stateChanged(ChangeEvent e)
	{
		betValue.setText(betSlider.getValue()+"");
		tokens.setText((myTokens-(betSlider.getValue()-myBet))+"");
		if(betSlider.getValue()>maxBet && betSlider.getValue()!=betSlider.getMaximum())
		{
			bet.setEnabled(true);
			raise.setEnabled(true);
			check.setEnabled(false);
			call.setEnabled(false);
		}
		else if(betSlider.getValue()==betSlider.getMaximum())
		{
			bet.setEnabled(false);
			raise.setEnabled(false);
			check.setEnabled(false);
			call.setEnabled(false);
		}
		else
		{
			bet.setEnabled(false);
			raise.setEnabled(false);
			check.setEnabled(true);
			call.setEnabled(true);
		}
	}
	public void turn(int myBet, int maxBet)
	{
		try
		{
			myTokens=Integer.parseInt(tokens.getText());
			this.myBet=myBet;
			this.maxBet=maxBet;
			if(maxBet<myTokens)
			{
				betSlider.setMinimum(maxBet);
				betSlider.setMaximum(myTokens);
				betSlider.setValue(maxBet);
			}
			else
			{
				
			}
			if(maxBet==0 || maxBet==myBet) firstBetButtons();
			else if(maxBet>=myTokens) toLittleTokensButtons();
			else normalAuctionButtons();
		}
		catch(NumberFormatException ex)
		{
			System.out.println("Wystapil problem z zamiana ciagu znakow na liczbe");
		}
	}
	public void newDeal(int myTokens, int function, int smallBlind)
	{
		this.myTokens=myTokens;
		tokens.setText(myTokens+"");
		this.smallBlind=smallBlind;
		if(function==1) specialF.setIcon(imgDealerButton);
		else if(function==2)
		{
			specialF.setIcon(imgSmallBlind);
			blind(smallBlind);
		}
		else if(function==3)
		{
			specialF.setIcon(imgBigBlind);
			blind(2*smallBlind);
		}
		else specialF.setIcon(null);
	}
	public void newDeal(int myTokens, int function)
	{
		this.myTokens=myTokens;
		tokens.setText(""+myTokens);
		if(function==1) specialF.setIcon(imgDealerButton);
		else if(function==2)
			{
				specialF.setIcon(imgSmallBlind);
				blind(smallBlind);
			}
		else if(function==3)
		{
			specialF.setIcon(imgBigBlind);
			blind(2*smallBlind);
		}
		else specialF.setIcon(null);
	}
	private void toLittleTokensButtons()
	{
		betSlider.setEnabled(false);
		allIn.setEnabled(true);
		fold.setEnabled(true);
		bet.setVisible(false);
		check.setVisible(false);
		call.setVisible(false);
		raise.setVisible(false);
	}
	private void firstBetButtons()
	{
		betSlider.setEnabled(true);
		allIn.setEnabled(true);
		fold.setEnabled(true);
		bet.setVisible(true);
		check.setVisible(true);
		bet.setEnabled(false);
		check.setEnabled(true);
		call.setVisible(false);
		raise.setVisible(false);
	}
	private void normalAuctionButtons()
	{
		betSlider.setEnabled(true);
		allIn.setEnabled(true);
		fold.setEnabled(true);
		bet.setVisible(false);
		check.setVisible(false);
		call.setVisible(true);
		raise.setVisible(true);
		call.setEnabled(true);
		raise.setEnabled(false);
	}
	private void blind(int value)
	{
		if(value<myTokens)
		{
			betSlider.setMinimum(0);
			betSlider.setMaximum(myTokens);
			betSlider.setValue(value);
			myBet=value;
			betValue.setText(myBet+"");
			myTokens-=value;
			tokens.setText(myTokens+"");
			bet.setEnabled(true);
			allIn.setEnabled(false);
		}
		else
		{
			betSlider.setValue(myTokens);
			myBet=myTokens;
			betValue.setText(myBet+"");
			myTokens=0;
			tokens.setText(myTokens+"");
			bet.setEnabled(false);
			allIn.setEnabled(true);
		}
		betSlider.setEnabled(false);
		fold.setEnabled(false);
		bet.setVisible(true);
		check.setVisible(false);
		call.setVisible(false);
		raise.setVisible(false);
	}
}
